#include <stdio.h>
#include <pthread.h>

#define NUM (10)

int x[NUM] = {0};

pthread_barrier_t barrier;

void* race(void *arg) 
{
        long i = (long)arg;
        ++ x[i];
        pthread_barrier_wait(&barrier);
        ++i;
        -- x[i%NUM];

        return NULL;
}

main() {
        pthread_t child[NUM];
        long i;

        pthread_barrier_init(&barrier, NULL, NUM);

        for(i=1; i<NUM; ++i) 
                if(pthread_create(&child[i], NULL, race, (void*)i) < 0)
                        perror("pthread_create");
        ++x[0];
        pthread_barrier_wait(&barrier);
        --x[1];

        for(i=1; i<NUM; ++i) 
                if(pthread_join(child[i], NULL) < 0)
                        perror("pthread_join");
        for(i=0; i<NUM; ++i)
                fprintf(stderr, "x[%d] = %d\n", i, x[i]);
}
